﻿//-----------------------------------------------------------------------
// <copyright file="EmailManager.cs" company="In The Hand Ltd">
//     Copyright © 2014 In The Hand Ltd. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
#if WINDOWS_PHONE
using Microsoft.Phone.Tasks;
#endif

namespace InTheHand.ApplicationModel.Email
{
    /// <summary>
    /// Allows an application to launch the email application with a new message displayed.
    /// Use this to allow users to send email from your application.
    /// </summary>
    public static class EmailManager
    {
        /// <summary>
        /// Launches the email application with a new message displayed.
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static async Task ShowComposeNewEmailAsync(InTheHand.ApplicationModel.Email.EmailMessage message)
        {
            if(message == null)
            {
                throw new ArgumentNullException();
            }

#if SILVERLIGHT
            // On Phone 8.0 use the email compose dialog
            EmailComposeTask task = new EmailComposeTask();

            if (!string.IsNullOrEmpty(message.Subject))
            {
                task.Subject = message.Subject;
            }

            if(!string.IsNullOrEmpty(message.Body))
            {
                task.Body = message.Body;
            }

            if (message.To.Count == 0)
            {
                throw new InvalidOperationException();
            }
            else
            {
                task.To = FormatRecipientCollection(message.To);
            }

            if (message.CC.Count > 0)
            {
                task.Cc = FormatRecipientCollection(message.CC);
            }

            if (message.Bcc.Count > 0)
            {
                task.Bcc = FormatRecipientCollection(message.Bcc);
            }

            task.Show();
#else
            // build URI for Windows Store platform

            // build a uri
            StringBuilder sb = new StringBuilder();
            bool firstParameter = true;

            if (message.To.Count == 0)
            {
                throw new InvalidOperationException();
            }
            else
            {
                sb.Append("mailto:" + FormatRecipientCollection(message.To));
            }

            if (message.CC.Count > 0)
            {
                if (firstParameter)
                {
                    sb.Append("?");
                    firstParameter = false;
                }
                else
                {
                    sb.Append("&");
                }

                sb.Append("cc=" + FormatRecipientCollection(message.CC));
            }

            if (message.Bcc.Count > 0)
            {
                if (firstParameter)
                {
                    sb.Append("?");
                    firstParameter = false;
                }
                else
                {
                    sb.Append("&");
                }

                sb.Append("bbc=" + FormatRecipientCollection(message.Bcc));
            }

            if (!string.IsNullOrEmpty(message.Subject))
            {
                if (firstParameter)
                {
                    sb.Append("?");
                    firstParameter = false;
                }
                else
                {
                    sb.Append("&");
                }

                sb.Append("subject=" + Uri.EscapeDataString(message.Subject));
            }

            if (!string.IsNullOrEmpty(message.Body))
            {
                if (firstParameter)
                {
                    sb.Append("?");
                    firstParameter = false;
                }
                else
                {
                    sb.Append("&");
                }

                sb.Append("body=" + Uri.EscapeDataString(message.Body));
            }

            await Windows.System.Launcher.LaunchUriAsync(new Uri(sb.ToString()));   
#endif
        }

        private static string FormatRecipientCollection(IList<EmailRecipient> list)
        {
            StringBuilder builder = new StringBuilder();

            foreach(EmailRecipient recipient in list)
            {
                if(!string.IsNullOrEmpty(recipient.Address))
                {
                    builder.Append(recipient.Address + ",");
                }
            }

            if(builder.Length > 0)
            {
                // trim final comma
                builder.Length--;
            }

            return builder.ToString();
        }
    }
}
